package cn.wolfcode.web.controller;

import cn.wolfcode.web.annotation.RequiredPermission;
import cn.wolfcode.web.domain.Department;
import cn.wolfcode.web.jsonResult.JsonResult;
import cn.wolfcode.web.query.QueryObject;
import cn.wolfcode.web.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //    @RequiredPermission(value="部门列表",expression = "department:list")
    @RequestMapping("/list")
    @RequiredPermission(value = "部门列表", expression = "department:list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        List<Department> departmentList = departmentService.listAll();
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("departmentList", departmentList);
        return "department/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(value = "部门删除", expression = "department:delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                departmentService.delete(id);
                System.out.println("xxxx");
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
    }
/*     @RequestMapping("/delete")
    @RequiredPermission(value = "部门删除",expression = "department:delete")
 public string delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list.do";
*/


    @RequestMapping("/input")
    public String input(Long id, Model model) {
        System.out.println("11");
        if (id != null) {
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        return "department/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(value = "部门保存或更新", expression = "department:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department, Model model) {

        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new JsonResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }


       /* System.out.println("11");
        if (department.getId() != null) {
            departmentService.update(department);
        } else {
            departmentService.save(department);
        }
        return "redirect:/department/list.do";//"/department/saveOrUpdate.do"*/
    }
}
